Home:ALL Converter>Outputting the contents of an editText to an array to be displayed in a textView

Outputting the contents of an editText to an array to be displayed in a textView

Ask Time:2020-02-24T01:11:54         Author:Ryan Murphy

Json Formatter

I am having trouble with outputting data from my editText into a textView. My application asks for an input from the user and then displays it by concatenating them together with other words and displayed in one String in a textView at the end, although for testing purposes I have made the textView visible at all timed. The next word is asked for by using a button. The problem is that I am only able to display one element of the array at a time. For example if I enter the word "table" into my editText and then press the button. The textView will show "there is a table that is ". Once i enter another word such as "yellow" and press the button it shows "there is a that is yellow"

Here is my code

public class MainActivity extends AppCompatActivity {


    int i = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        chooseGame();

    }

    public String enterWord() {


        String[] array = {"Adjective", "Adjective", "Proper Noun", "Name"};
        String entry;
        if (array[i].equals("Adjective")) {

            entry = "Enter an ";
        } else {
            entry = "Enter a ";
        }
        return entry;

    }


    public void nextWord(View view) { //nextWord is the cnclick of the button


        i++;
        getNextWord();

    getInputs();



    }



    public void getNextWord() {
        String[] array = {"Adjective", "Adjective", "Proper Noun", "Name"};
        TextView wordTextView = findViewById(R.id.wordTextView);
        Button nextButton = findViewById(R.id.nextButton);

        EditText editTextView = findViewById(R.id.enterEditText);
        String displayText = enterWord() + array[i];
        String displayHint = array[i];

        wordTextView.setText(displayText);
        editTextView.setHint(displayHint);


        }

    }

    public void chooseGame() {


        final ArrayList<String> arrayList = new ArrayList<String>();

        final TextView wordTextView = findViewById(R.id.wordTextView);
        final EditText editTextView = findViewById(R.id.enterEditText);
        final Button nextButton = findViewById(R.id.nextButton);


        ArrayList<String> gamesArrayList = new ArrayList<String>();
        gamesArrayList.add("A Vegan's Worst Nightmare");
        gamesArrayList.add("The Wet Floor Sign");
        gamesArrayList.add("The Meaning of life");
        gamesArrayList.add("Campfire Story");
        gamesArrayList.add("The Crocobearamouse");


        final ListView gamesListView = findViewById(R.id.gamesListView);

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                gamesArrayList);

        gamesListView.setAdapter(arrayAdapter);
        gamesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                                 @Override
                                                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                     if (position <= 5 && position >= 0) {

                                                         gamesListView.setVisibility(View.INVISIBLE);

                                                         editTextView.setVisibility((View.VISIBLE));
                                                         wordTextView.setVisibility((View.VISIBLE));
                                                         nextButton.setVisibility(View.VISIBLE);

                                                     }

                                                 }


                                             }
        );


    }

        public void getInputs() {
    TextView finalTextView = findViewById(R.id.finalTextView);
            EditText editTextView = findViewById(R.id.enterEditText);
            String[] inputsArray = {"","","","",""};
       inputsArray[i] = editTextView.getText().toString();
       String inputs = "There is a " + inputsArray[0] + " that is" + inputsArray[1];

        finalTextView.setText(inputs);


    }



    }

Here is my XML

  <TextView

        android:id="@+id/wordTextView"
        android:layout_width="418dp"
        android:layout_height="146dp"
        android:layout_marginTop="84dp"
        android:text="Enter a noun"
        android:gravity="center"
        android:textSize="30sp"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/enterEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="244dp"
        android:ems="10"
        android:hint="Noun"
        android:visibility="invisible"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/wordTextView" />

    <Button
        android:id="@+id/nextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="180dp"
        android:onClick="nextWord"
        android:text="Next"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/enterEditText"
        app:layout_constraintVertical_bias="0.0" />

    <ListView
        android:id="@+id/gamesListView"
        android:layout_width="404dp"
        android:layout_height="647dp"
        tools:layout_editor_absoluteX="3dp"
        tools:layout_editor_absoluteY="84dp" />

    <TextView
        android:id="@+id/finalTextView"
        android:layout_width="157dp"
        android:layout_height="98dp"
        app:layout_constraintBottom_toTopOf="@+id/enterEditText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Author:Ryan Murphy,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60364728/outputting-the-contents-of-an-edittext-to-an-array-to-be-displayed-in-a-textview
yy